﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using FaceSharp.Samples.Presenter;
using FaceSharp.Samples.View;

namespace FaceSharp.Samples.WinForm
{
    public partial class PhotoBrowser : Form, View.IPhotoAlbum
    {
        private  PhotoBrowserPresenter _photoBrowserPresenter;

        public PhotoBrowser()
        {
            InitializeComponent();
        }

        public IEnumerable<AlbumDetail> Albums
        {
            set {
                List<AlbumDetail> list = value.ToList();
                albumComboBox.DataSource = list;
                albumComboBox.DisplayMember = "AlbumName";
                albumComboBox.ValueMember = "AlbumId";
            }
        }

        public string AlbumId
        {
            get { return albumComboBox.SelectedValue.ToString(); }
        }

        public string AlbumName
        {
            set { albumLabel.Text = "Selected Album: " + value; }
        }

        public IEnumerable<PhotoDetail> Photos
        {
            set
            {
                photoListView.Items.Clear();
                photoListView.View = System.Windows.Forms.View.Tile;
                photoListView.TileSize = new System.Drawing.Size(400, 120);
                photoListView.UseCompatibleStateImageBehavior = false;
                var imageListLarge = new ImageList();

                foreach (var photoDetail in value)
                {
                    imageListLarge.Images.Add(LoadImage(photoDetail.PhotoUrl));
                }

                imageListLarge.ImageSize = new Size(100, 100);
                photoListView.LargeImageList = imageListLarge;
                int i = 0;

                foreach (var photoDetail in value)
                {
                    photoListView.Items.Add(photoDetail.Caption, i);
                    i++;
                }
            }
        }

        public static Image LoadImage(string url)
        {
            var backgroundUrl = url;
            WebRequest request = WebRequest.Create(backgroundUrl);
            WebResponse response = request.GetResponse();
            System.IO.Stream stream = response.GetResponseStream();
            return Image.FromStream(stream);
        }

        private void PhotoBrowser_Load(object sender, EventArgs e)
        {
            _photoBrowserPresenter = new PhotoBrowserPresenter(this);
        }

        private void viewPhotosButton_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            _photoBrowserPresenter.ViewPhotos();
            this.Cursor = Cursors.Default;
        }

        private void PhotoBrowser_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

    }
}
